﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using PSE.Deployment.SetupBase.Configuration;
using PSE.Deployment.SetupBase.Configuration.Actions;
using PSE.Deployment.SetupBase.Configuration.Enums;
using System.Diagnostics;

namespace PSE.Deployment.PSESetupGenerator
{
    public class Program
    {
        private static SetupConfiguration config;
        private static SystemInfo _systemInfo;
        private static string _outPutPath;

        public static void Main(string[] args)
        {
            try
            {
                _systemInfo = new SystemInfo();
                _systemInfo.Id = "PSE";
                _systemInfo.Description = "Plataforma de Serviços v4";

                if (args.Length == 0)
                {
                    if (Debugger.IsAttached)
                    {
                        _systemInfo.Version = new PSE.Framework.Common.Version(4, 0, 0, 0);
                        _outPutPath = @"C:\DSEPROJ\Compilados\PSE\Deployment\";
                    }
                    else
                    {
                        throw new Exception("Invalid arguments!");
                    }
                }
                else if (args.Length == 2)
                {
                    _systemInfo.Version = new PSE.Framework.Common.Version(args[0]);
                    _outPutPath = args[1];
                }
                else
                {
                    throw new Exception("Invalid arguments!");
                }

                PopulateConfig();
            }
            catch (Exception ex)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine(ex.Message);
            }
        }

        private static void PopulateConfig()
        {
            config = new SetupConfiguration();

            config.SystemInfo = _systemInfo.Clone();

            config.DefaultInstallPath = @"BMFBOVESPA\";

            Group groupDependencyFwkPSE = new Group();
            groupDependencyFwkPSE.System = _systemInfo.Clone();
            groupDependencyFwkPSE.Id = "PSE-Framework";
            groupDependencyFwkPSE.Name = "PSE-Framework";
            groupDependencyFwkPSE.Description = "Framework da plataforma de serviços";
            groupDependencyFwkPSE.Layers.Add(PSE.Deployment.SetupBase.Configuration.Enums.Layer.Client);
            groupDependencyFwkPSE.Layers.Add(PSE.Deployment.SetupBase.Configuration.Enums.Layer.Server);
            groupDependencyFwkPSE.Module.Id = "Framework";
            groupDependencyFwkPSE.Module.Name = "Framework";
            groupDependencyFwkPSE.Index = 0;
            AddActionCopy(groupDependencyFwkPSE, "Copia dos arquivos do framework da plataforma de serviços", @"Framework\v4\", @"Certificates\", @"Settings\");
            AddActionInstallAssembly(groupDependencyFwkPSE, "Instalação dos arquivos do framework da plataforma de serviços", @"Framework\v4\");
            AddActionInstallPSECertificate(groupDependencyFwkPSE, "Instalação dos certificados da plataforma de serviços");
            AddCreateShortcut(groupDependencyFwkPSE, "Criação dos atalhos");
            AddActionUpdateWindowsRegistry(groupDependencyFwkPSE, "Criação do registro de windows");
            config.SystemInfo.Groups.Add(groupDependencyFwkPSE);

            ((Copy)groupDependencyFwkPSE.Actions[0]).Folders.Add(new FolderInfo()
            {
                Source = @"Bin\PSE\",
                Destination = @"PSE\",
                Files = new List<string> { "GeneralSettings.config" }
            });

            PSE.Deployment.Util.Serializer.Serialize<SetupConfiguration>(config, Path.Combine(_outPutPath, "SetupConfiguration.xml"));
        }

        private static void AddActionCopy(Group group, string description, params string[] destinations)
        {
            Copy c = new Copy();
            c.Id = "Copy-" + group.Id;
            c.Description = description;
            c.GroupId = group.Id;
            c.Index = 0;

            foreach (string destination in destinations)
            {
                c.Folders.Add(new FolderInfo()
                {
                    Source = @"Bin\PSE\" + destination,
                    Destination = @"PSE\" + destination,
                });
            }

            group.Actions.Add(c);
        }

        private static void AddActionInstallAssembly(Group group, string description, params string[] destinations)
        {
            InstallAssembly c = new InstallAssembly();
            c.Id = "InstallAssembly-" + group.Id;
            c.Description = description;
            c.GroupId = group.Id;
            c.Index = 1;

            foreach (string destination in destinations)
            {
                c.Folders.Add(new FolderInfo()
                {
                    Source = @"Bin\PSE\" + destination,
                    Destination = @"PSE\" + destination,
                });
            }

            group.Actions.Add(c);
        }

        private static void AddActionInstallPSECertificate(Group group, string description)
        {
            InstallCertificate c = new InstallCertificate();
            c.Id = "InstallCertificate-" + group.Id;
            c.Description = description;
            c.GroupId = group.Id;
            c.Index = 2;

            c.Certificates.Add(new PSE.Deployment.SetupBase.Configuration.Certificate()
            {
                StoreName = System.Security.Cryptography.X509Certificates.StoreName.AuthRoot,
                Password = "A5MKUVMFnmKAZsTnKvFG9yXcx4y/Eet6",
                File = new PSE.Deployment.SetupBase.Configuration.FileInfo()
                {
                    Source = @"Bin\PSE\Certificates\",
                    Destination = @"PSE\Certificates\",
                    FileName = "BVMFPSERootCA.pfx"
                }
            });

            group.Actions.Add(c);
        }

        private static void AddCreateShortcut(Group group, string description)
        {
            CreateShortcut c = new CreateShortcut();
            c.Id = "CreateShortcut-" + group.Id;
            c.Description = description;
            c.GroupId = group.Id;
            c.Index = 10;

            c.Shortcuts.Add(new Shortcut()
            {
                Executable = new PSE.Deployment.SetupBase.Configuration.FileInfo()
                {
                    Destination = @"PSE\Setup-v4\",
                    FileName = "SetupUninstall.exe",
                },
                Icon = new PSE.Deployment.SetupBase.Configuration.FileInfo()
                {
                    Destination = @"PSE\Setup-v4\",
                    FileName = "PSE.ico",
                },
                ShotcutFile = new PSE.Deployment.SetupBase.Configuration.FileInfo()
                {
                    Destination = @"Plataforma de Serviços\Framework\",
                    FileName = "Setup 4.lnk",
                },
                SpecialFolders = new List<PSE.Deployment.SetupBase.Configuration.Enums.SpecialFolder>() { PSE.Deployment.SetupBase.Configuration.Enums.SpecialFolder.StartMenu }
            });

            c.Shortcuts.Add(new Shortcut()
            {
                Executable = new PSE.Deployment.SetupBase.Configuration.FileInfo()
                {
                    Destination = @"PSE\Framework\v4\Versions\",
                    FileName = "PSEVersions.exe",
                    Arguments = "4"
                },
                Icon = new PSE.Deployment.SetupBase.Configuration.FileInfo()
                {
                    Destination = @"PSE\Framework\v4\Versions\",
                    FileName = "Versions.ico",
                },
                ShotcutFile = new PSE.Deployment.SetupBase.Configuration.FileInfo()
                {
                    Destination = @"Plataforma de Serviços\Framework\",
                    FileName = "Visualizador de Versões PSE.lnk",
                },
                SpecialFolders = new List<PSE.Deployment.SetupBase.Configuration.Enums.SpecialFolder>() { PSE.Deployment.SetupBase.Configuration.Enums.SpecialFolder.StartMenu }
            });

            group.Actions.Add(c);
        }

        private static void AddActionUpdateWindowsRegistry(Group group, string description)
        {
            UpdateWindowsRegistry c = new UpdateWindowsRegistry();
            c.Id = "UpdateWindowsRegistry-" + group.Id;
            c.Description = description;
            c.GroupId = group.Id;
            c.Index = 10;

            c.Keys.Add(new RegistryKey()
            {
                Name = @"SOFTWARE\PSE",
                Values = new List<RegistryValue>() 
                { 
                    new RegistryValue() { Name = "ApplicationRootDirectory", Description="ApplicationRootDirectory", XPath = "/SetupParameter/@FullInstallationPath", RegistryValueKind = Microsoft.Win32.RegistryValueKind.String , IsVisible = false, Type = XmlTagType.String }
                }
            });

            c.Keys.Add(new RegistryKey()
            {
                Name = @"SOFTWARE\PSE",
                Values = new List<RegistryValue>() 
                { 
                    new RegistryValue() { Name = "SystemDescription", Description="SystemDescription", Value = "PSE", RegistryValueKind = Microsoft.Win32.RegistryValueKind.String , IsVisible = false, Type = XmlTagType.String }
                }
            });

            group.Actions.Add(c);
        }
    }
}
